package com.xdf.docmanagementsystem.controller;

import com.xdf.docmanagementsystem.common.base.BaseController;
import com.xdf.docmanagementsystem.common.util.IOUtil;
import com.xdf.docmanagementsystem.enums.ResponseEnum;
import com.xdf.docmanagementsystem.service.ViewFactory;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.xdf.docmanagementsystem.entity.ResponseResult;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import com.xdf.docmanagementsystem.entity.DocExtraFile;
import com.xdf.docmanagementsystem.service.DocExtraFileService;

import javax.servlet.http.HttpServletResponse;

/**
 * @Description : 自动生成的控制层
 * @Author : ZK
 * @Date : 2020-06-23 11:00:30
 */
@Api(tags = "文档附件")
@CrossOrigin
@RestController
@RequiresUser
@RequestMapping("/docExtraFile")
public class DocExtraFileController extends BaseController {

    @Autowired
    private DocExtraFileService service;

    /**
     * @Description : 选择新增
     * @Return : ResponseResult<Void>
     * @Author : ZK
     * @Date : 2020-06-23 11:00:30
     */
    @ApiOperation(value = "选择新增", httpMethod = "POST", notes = "自动生成的新增方法")
    @RequestMapping(value = "/insertSelective", method = RequestMethod.POST)
    public ResponseResult<Void> insertSelective(DocExtraFile record) throws Exception {
        service.insertSelective(record);
        return new ResponseResult<Void>(true, ResponseEnum.OK.getState(), ResponseEnum.OK.getMessage());
    }

    /**
     * @Description : 批量新增
     * @Return : ResponseResult<Void>
     * @Author : ZK
     * @Date : 2020-06-23 11:00:30
     */
    @ApiOperation(value = "批量新增", httpMethod = "POST", notes = "自动生成的新增方法")
    @RequestMapping(value = "/multiInsert", method = RequestMethod.POST)
    public ResponseResult<List<DocExtraFile>> multiInsert(@RequestBody List<DocExtraFile> list) throws Exception {
        if (list != null && list.size() > 0) {
            service.multiInsert(list);
        }
        return new ResponseResult<List<DocExtraFile>>(true, ResponseEnum.OK.getMessage());
    }

    /**
     * @Description : 选择修改
     * @Return : DocExtraFile
     * @Author : ZK
     * @Date : 2020-06-23 11:00:30
     */
    @ApiOperation(value = "选择修改", httpMethod = "PUT", notes = "自动生成的修改方法")
    @RequestMapping(value = "/updateSelective", method = RequestMethod.POST)
    public ResponseResult<Void> updateSelective(DocExtraFile record) throws Exception {
        service.updateByPrimaryKeySelective(record);
        return new ResponseResult<Void>(true, ResponseEnum.OK.getState(), ResponseEnum.OK.getMessage());
    }

    /**
     * @Description : 批量修改
     * @Return : DocExtraFile
     * @Author : ZK
     * @Date : 2020-06-23 11:00:30
     */
    @ApiOperation(value = "批量修改", httpMethod = "POST", notes = "自动生成的修改方法")
    @RequestMapping(value = "/multiUpdate", method = RequestMethod.POST)
    public ResponseResult<List<DocExtraFile>> multiUpdate(@RequestBody List<DocExtraFile> list) throws Exception {
        service.multiUpdate(list);
        return new ResponseResult<List<DocExtraFile>>(true, ResponseEnum.OK.getState(), ResponseEnum.OK.getMessage());
    }

    /**
     * @Description : 根据主键查询
     * @Return : DocExtraFile
     * @Author : ZK
     * @Date : 2020-06-23 11:00:30
     */
    @ApiOperation(value = "根据主键查询", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequestMapping(value = "/queryByPk/{id}", method = RequestMethod.GET)
    public DocExtraFile selectByPrimaryKey(@PathVariable("id") Integer id) throws Exception {
        if (id == null) return null;
        return service.selectByPrimaryKey(id);
    }

    /**
     * @Description : 查询方法
     * @Return : java.util.List
     * @Author : ZK
     * @Date : 2020-06-23 11:00:30
     */
    @ApiOperation(value = "查询方法", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequestMapping(value = "/listQuery", method = RequestMethod.GET)
    public List<DocExtraFile> query(DocExtraFile record) throws Exception {
        return service.listQuery(record);
    }

    /**
     * @Description : 根据主键删除方法
     * @Return : ResponseResult<Void>
     * @Author : ZK
     * @Date : 2020-06-23 11:00:30
     */
    @ApiOperation(value = "根据主键删除方法", httpMethod = "DELETE", notes = "自动生成的删除方法")
    @RequestMapping(value = "/deleteByPk/{id}", method = RequestMethod.POST)
    public ResponseResult<Void> deleteByPrimaryKey(@PathVariable("id") Integer id) throws Exception {
        service.deleteByPrimaryKey(id);
        return new ResponseResult<Void>(true, ResponseEnum.OK.getState(), ResponseEnum.OK.getMessage());
    }

    /**
     * 预览文件
     *
     * @param id
     * @param response
     */
    @GetMapping("/view/{id}")
    public void view(@PathVariable("id") Integer id, HttpServletResponse response) throws Exception {
        Assert.state(id != null, ResponseEnum.NOT_ALLOW.getMessage());
        DocExtraFile docExtraFile = service.selectByPrimaryKey(id);
        Assert.notNull(docExtraFile, ResponseEnum.NOT_ALLOW.getMessage());
        File file = new File(new StringBuilder()
                .append(getCurrentAppParentPath())
                .append(File.separator)
                .append(docExtraFile.getFilePath())
                .toString());
        Assert.state(file.exists(), ResponseEnum.ALREADY_DELETE.getMessage());
        response.setCharacterEncoding("UTF-8");
        ViewFactory.getFactory(docExtraFile.getFileType()).view(response.getOutputStream(), file);
    }

    /**
     * 下载文件
     *
     * @param id
     * @param response
     * @throws IOException
     */
    @GetMapping("/download/{id}")
    public void fileDownload(@PathVariable("id") Integer id, HttpServletResponse response) throws Exception {
        Assert.state(id != null, ResponseEnum.NOT_ALLOW.getMessage());
        DocExtraFile docExtraFile = service.selectByPrimaryKey(id);
        Assert.notNull(docExtraFile, ResponseEnum.NOT_ALLOW.getMessage());
        File file = new File(new StringBuilder()
                .append(getCurrentAppParentPath())
                .append(File.separator)
                .append(docExtraFile.getFilePath())
                .toString());
        Assert.state(file.exists(), ResponseEnum.ALREADY_DELETE.getMessage());
        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode(file.getName(), "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        IOUtil.writeFile(file, response.getOutputStream());
    }

}